package com.trimps.steps.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.trimps.steps.pojo.TotalSteps;
import com.trimps.steps.pojo.WalkerGroup;
import com.trimps.steps.service.IStepService;
import com.trimps.steps.service.IWalkerGroupService;
import com.trimps.steps.service.IWalkerService;

@Controller
@RequestMapping(value = "report/")
public class ReportController extends BaseController {
    private static final Logger logger = Logger.getLogger(ReportController.class);

    @Autowired
    private IStepService stepService;
    @Autowired
    private IWalkerService walkerService;
    @Autowired
    private IWalkerGroupService walkerGroupService;

    /**
     * 
     * @param page
     * @param map
     *            必须这样写：@RequestParam HashMap<String, String> map,否则无法映射值,
     *            jsp页面可以通过 ${map.myname }获取
     * @param modelMap
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getReportListData", method = RequestMethod.POST)
    public Map<String, Object> getReportListData(
                    @RequestParam(required = false) String realyName,
                    @RequestParam(required = false) String startTime,
                    @RequestParam(required = false) String endTime,
                    @RequestParam(required = false) String groupId,
                    @RequestParam(required = false) String sort) {

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("realyName", realyName);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("groupId", groupId);

        String orderBy = "";
        if (StringUtils.isNotEmpty(sort)) {
            orderBy = sort.equals("0") ? " DESC" : " ASC";
            if (StringUtils.isNotEmpty(orderBy)) {
                map.put("orderBy", orderBy);
            }
        }
        
        Map<String, Object> totalStepsMmap = new HashMap<>();

        List<TotalSteps> steps = stepService.getTotalSteps(map);
        boolean hasContent = false;
        if(steps != null && !steps.isEmpty() ){
            hasContent = true;
            totalStepsMmap.put("hasContent", hasContent);
        }
       
        totalStepsMmap.put("steps", steps);
        
        return totalStepsMmap;
    }

    @ResponseBody
    @RequestMapping(value = "getGroupsName", method = RequestMethod.POST)
    public Map<String, Object> getGroupsName() {
        Map<String, Object> map = new HashMap<>();
        List<WalkerGroup> groups = walkerGroupService.getAllWalkerGroups();
        if (getUserId() != null) {
            List<WalkerGroup> ownGroups = walkerGroupService.getWalkerGroupByUserId(getUserId());
            map.put("ownGroups", ownGroups);
        }
        map.put("groups", groups);
        return map;
    }
}
